Skip to content

Conversation

shulaoda
Copy link
Contributor

@shulaoda shulaoda commented Oct 8, 2025

closes #6660, #6586, #6600

Previously, when formatting comments containing blockquotes followed by comparison operators like > >= >> >>= >>> >>>=, the code would panic with byte index out of bounds error.

The issue was in itemized_block_quote_start function, which counted all > characters in non-alphanumeric prefix, treating operators like >= as nested blockquote markers. This caused the calculated indent to exceed the line length, leading to out-of-bounds indexing.

Fixed by only recognizing valid Markdown blockquote syntax where each > must be followed by a space (e.g., > > text), which was the original intended behavior. Note that only the standard format with spaces is supported, alternative formats like >> text, > > text (multiple spaces), or >text (no space) are not recognized as blockquote markers.

@shulaoda shulaoda marked this pull request as draft October 9, 2025 02:20
@shulaoda shulaoda marked this pull request as ready for review October 9, 2025 03:55
@shulaoda shulaoda requested a review from Manishearth October 9, 2025 03:55
@fee1-dead fee1-dead self-assigned this Oct 10, 2025
@shulaoda shulaoda marked this pull request as draft October 13, 2025 02:02
@shulaoda shulaoda force-pushed the 10-09-fix-issue-6660 branch from fa5abb7 to 608078c Compare October 13, 2025 02:08
src/comment.rs Outdated
// of the new line_start. We update the indent because it effects the max width
// of each formatted line.
line_start = itemized_block_quote_start(line, line_start, 2);
line_start = itemized_block_quote_start(line.trim_start(), line_start, 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to line.trim_start() because the previous implementation could cause the quote level calculation

let quote_level = line
    .chars()
    .take_while(|&c| matches!(c, '>' | ' '))
    .filter(|&c| c == '>')
    .count();

to include special whitespace characters (e.g., '\x09') in the prefix.

Copy link
Member

@fee1-dead fee1-dead Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this implementation is actually slightly incorrect.

A tab is treated as four space characters, which cannot start a blockquote.

A nested blockquote can have four spaces between them:

> > is valid, but > > is not.

A blockquote can start with up to three spaces:

> is fine to start a blockquote, but > will cause it to be treated as a code block.

See https://spec.commonmark.org/0.31.2/#block-quote-marker

Copy link
Contributor Author

@shulaoda shulaoda Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have not addressed my comment: please parse blockquotes according to the spec

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have not addressed my comment: please parse blockquotes according to the spec

Makes sense — I’ll fix this in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, this might break some of the existing styling and cause visual differences. I’m not sure if it should be addressed in this PR, but I’ll try to fix it for now anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break some of the existing styling

isn't this an unstable formatter option anyways?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break some of the existing styling

isn't this an unstable formatter option anyways?

Right, that makes sense — nothing to worry about then!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fee1-dead I haven’t handled the second case yet — > is fine to start a blockquote, but > will cause it to be treated as a code block.

This can be determined correctly when the previous line isn’t a list item (-, *, +, 1., or 1)), but issues arise in cases like the one below, since the > actually belongs to the -, and its indentation is determined by the list item. There are many such combinations, and for now, I’ve only handled the > > case.

   - aaa
     6666
     > bbb

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering all the cases would be too complex and require significant refactoring. For now, can we focus on fixing the blockquote panic issue only?

@shulaoda shulaoda requested a review from fee1-dead October 13, 2025 02:16
@shulaoda shulaoda marked this pull request as ready for review October 13, 2025 02:16
src/comment.rs Outdated
// of the new line_start. We update the indent because it effects the max width
// of each formatted line.
line_start = itemized_block_quote_start(line, line_start, 2);
line_start = itemized_block_quote_start(line.trim_start(), line_start, 1);
Copy link
Member

@fee1-dead fee1-dead Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this implementation is actually slightly incorrect.

A tab is treated as four space characters, which cannot start a blockquote.

A nested blockquote can have four spaces between them:

> > is valid, but > > is not.

A blockquote can start with up to three spaces:

> is fine to start a blockquote, but > will cause it to be treated as a code block.

See https://spec.commonmark.org/0.31.2/#block-quote-marker

@shulaoda shulaoda force-pushed the 10-09-fix-issue-6660 branch from 608078c to 79304a8 Compare October 13, 2025 02:51
@shulaoda shulaoda requested a review from fee1-dead October 13, 2025 02:51
@shulaoda shulaoda marked this pull request as draft October 13, 2025 03:36
@shulaoda shulaoda marked this pull request as ready for review October 13, 2025 12:01
@shulaoda shulaoda force-pushed the 10-09-fix-issue-6660 branch from 99a8a9d to a443182 Compare October 13, 2025 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ICE with wrap_comments = true

4 participants